package com.example.hospitalmanagement.utils

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import com.example.hospitalmanagement.R

/**
 * 预约挂号中弹窗
 */
class AutoHideBottomUIDialog(context: Context) : Dialog(context) {
    private var mContext: Context = context

    private var height = 0f

    override fun onStart() {
        super.onStart()
        window?.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
        window?.decorView?.systemUiVisibility =
            (View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
    }

    @SuppressLint("ResourceType", "UseCompatLoadingForDrawables")
    override fun show() {
        this.window?.setFlags(
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        )
        this.window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
        val window = this.window
        if (height == 0f) {
            window?.attributes?.height = DisplayUtil.dp2px(mContext, 440f)
        } else {
            window?.attributes?.height = DisplayUtil.dp2px(mContext, height)
        }
        window?.attributes?.width = DisplayUtil.dp2px(mContext, 300f)
        window?.setBackgroundDrawable(mContext.resources.getDrawable(R.drawable.home_item_background))
        window?.setGravity(Gravity.CENTER)
        super.show()
    }

    fun setDialogHeight(width: Float): AutoHideBottomUIDialog {
        this.height = width
        return this
    }
}